package com.ruoyi.oto.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.MemberUser;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.oto.constants.DeleteFlag;
import com.ruoyi.oto.constants.MemberRoleConstant;
import com.ruoyi.oto.constants.TagType;
import com.ruoyi.oto.constants.TagVisible;
import com.ruoyi.oto.domain.BranchLink;
import com.ruoyi.oto.domain.CompanyCore;
import com.ruoyi.oto.domain.CompanyTags;
import com.ruoyi.oto.domain.CompanyTagsCore;
import com.ruoyi.oto.domain.vo.CompanyTagCoreQueryParamItem;
import com.ruoyi.oto.domain.vo.CompanyTagCoreQueryParamVO;
import com.ruoyi.oto.mapper.CompanyTagsMapper;
import com.ruoyi.oto.service.BranchLinkService;
import com.ruoyi.oto.service.CompanyTagsService;
import com.ruoyi.oto.tagmatcher.enums.TagMatcherNodeOpt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 企业标签关系
 *
 * @author ruoyi
 */
@Service
public class CompanyTagsServiceImpl extends ServiceImpl<CompanyTagsMapper, CompanyTags> implements CompanyTagsService
{
    @Autowired
    private BranchLinkService branchLinkService;

    @Override
    public List<CompanyTags> companyTagListFront(CompanyCore curtCompany, MemberUser curtMember, Integer applyStatus) {
        Long roleId = curtMember.getRoleId(); //当前登录用户的角色
        Long targetObjId = 0L;
        if(MemberRoleConstant.游客.equals(roleId)){
            targetObjId = 0L;
        }
        else if(MemberRoleConstant.企业用户.equals(roleId)){
            targetObjId = curtMember.getId();
        }
        else if(MemberRoleConstant.联企用户.equals(roleId)){
            targetObjId = curtMember.getId();
        }
        return this.getBaseMapper().companyTagList("front",curtCompany.getId(),roleId, targetObjId.toString(), applyStatus);
    }

    @Override
    public List<CompanyTags> companyTagListBack(CompanyCore curtCompany, SysUser sysUser, Integer applyStatus) {
        return this.getBaseMapper().companyTagList("back",curtCompany.getId(),null,sysUser.isAdmin() ? null : sysUser.getDeptId().toString(),applyStatus);
    }


    @Override
    public CompanyTags getCompanyTagsByCompanyAndTagId(Long companyId, Long tagId) {
        return this.getOne(new LambdaQueryWrapper<CompanyTags>().eq(CompanyTags::getCompanyId, companyId)
                .eq(CompanyTags::getTagId,tagId).eq(CompanyTags::getDelFlag, DeleteFlag.NO));
    }

    @Override
    public List<Long> getCompanyIdsByTagVal(List<String> conditions) {
        return this.baseMapper.getCompanyIdsByTagVal(conditions);
    }

    /**
     * 获取查询参数标签
     * @param loginUser
     * @return
     */
    @Override
    public CompanyTagCoreQueryParamVO getCompanyTagsQueryParam(LoginUser loginUser) {
        List<CompanyTagsCore> list = new ArrayList<>();
        if(loginUser.getUser()!=null && loginUser.getUser().isLogin()){
            //说明是后台系统查询,根据当前登录的用户获取不同的标签
            list = this.getBaseMapper().getCompanyTagsQueryParam("back",null,loginUser.getUser().isAdmin() ? null : loginUser.getUser().getDeptId().toString());
        }
        else if(loginUser.getMember()!=null){
            //说明是前端参数查询
            Long targetId = 0L;
            Long roleId = loginUser.getMember().getRoleId();
            if(loginUser.getMember().getRoleId()==MemberRoleConstant.游客){
                targetId = 0L;
            }
            else if(loginUser.getMember().getRoleId()==MemberRoleConstant.企业用户||loginUser.getMember().getRoleId()==MemberRoleConstant.联企用户){
                targetId = loginUser.getMember().getId();
            }
            list= this.getBaseMapper().getCompanyTagsQueryParam("front",roleId,targetId.toString());
        }
        List<CompanyTagsCore> textTags = new ArrayList<>();
        List<CompanyTagsCore> numericTags = new ArrayList<>();
        List<CompanyTagsCore> optionTags = new ArrayList<>();
        List<CompanyTagsCore> booleanTags = new ArrayList<>();
        list.forEach(companyTagsCore -> {
            if(companyTagsCore.getTagType()== TagType.文本){
                textTags.add(companyTagsCore);
            } else if (companyTagsCore.getTagType()== TagType.数字) {
                numericTags.add(companyTagsCore);
            } else if (companyTagsCore.getTagType()== TagType.选择) {
                optionTags.add(companyTagsCore);
            } else if (companyTagsCore.getTagType()== TagType.是否显示) {
                booleanTags.add(companyTagsCore);
            }
        });
        return new CompanyTagCoreQueryParamVO(
                new CompanyTagCoreQueryParamItem(textTags, Arrays.asList(TagMatcherNodeOpt.EQUAL.getDesc(),TagMatcherNodeOpt.NEQUAL.getDesc(),TagMatcherNodeOpt.LLIKE.getDesc(),TagMatcherNodeOpt.RLIKE.getDesc(),TagMatcherNodeOpt.LIKE.getDesc())),
                new CompanyTagCoreQueryParamItem(numericTags, Arrays.asList(TagMatcherNodeOpt.EQUAL.getDesc(),TagMatcherNodeOpt.NEQUAL.getDesc(),TagMatcherNodeOpt.GT.getDesc(),TagMatcherNodeOpt.GE.getDesc(),TagMatcherNodeOpt.LT.getDesc(),TagMatcherNodeOpt.LE.getDesc(),TagMatcherNodeOpt.BETWEEN.getDesc())),
                new CompanyTagCoreQueryParamItem(optionTags, Arrays.asList(TagMatcherNodeOpt.EQUAL.getDesc())),
                new CompanyTagCoreQueryParamItem(booleanTags,new ArrayList<>()));
    }
}
